package com.mdtx.entity;

import com.mdtx.database.convert.JavaMapping;
import com.mdtx.database.convert.MysqlConvertToJavaMapping;
import com.mdtx.database.convert.OracleConvertToJavaMapping;
import com.mdtx.database.type.DbType;
import com.mdtx.utils.ConvertUtil;

/**
 * 字段属性
 *
 * @author zhgz
 * @create 2017-07-23 20:32
 **/
public class FieldAttribute extends ColumnAttribute{

    /**
     * 字段类型
     */
    private String filedType;
    /**
     * 字段名称
     */
    private String fileName;
    /**
     * 注释
     */
    private String comment;

    private String dbType;


    public JavaMapping getJavaMapping(){
        if(dbType.equals(DbType.MYSQL)){
            return new MysqlConvertToJavaMapping();
        }else if(dbType.equals(DbType.ORACLE)){
            return new OracleConvertToJavaMapping();
        }
        return null;
    }

    public String getFiledType() {
        return ConvertUtil.toCamelCasing(this.getJavaMapping().getJavaType(super.getTypeName().toLowerCase()),false);
    }

    public String getFileName() {
        return ConvertUtil.toCamelCasing(super.getColumnName().toLowerCase(),false);
    }

    public String getComment() {
        return super.getRemark();
    }


    public String getDbType() {
        return dbType;
    }

    public void setDbType(String dbType) {
        this.dbType = dbType;
    }


    public void setFiledType(String filedType) {
        this.filedType = filedType;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        FieldAttribute that = (FieldAttribute) o;

        boolean b1 = that.getFileName()!=null && !"".equals(that.getFileName()) && that.getFileName().equals(this.getFileName());
        boolean b2 = that.getFiledType()!=null && !"".equals(that.getFiledType()) && that.getFiledType().equals(this.getFiledType());

        return b1 && b2;

    }

    @Override
    public int hashCode() {
        int result = filedType != null ? filedType.hashCode() : 0;
        result = 31 * result + (fileName != null ? fileName.hashCode() : 0);
        result = 31 * result + (comment != null ? comment.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        StringBuffer str = new StringBuffer();
        str.append("FieldAttribute{ filedType=").append(filedType )
                .append( ", fileName=" ).append(fileName).append( ", comment=" ).append(comment)
                .append("}");
        return str.toString();
    }
}
